<%#
 Copyright 2013-2020 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
import React, { useEffect } from 'react';
import { connect } from 'react-redux';
import { Link, RouteComponentProps } from 'react-router-dom';
import { Button, Row, Badge } from 'reactstrap';
import { Translate, TextFormat } from 'react-jhipster';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';

import { APP_DATE_FORMAT } from 'app/config/constants';
<% if (enableTranslation) { %>import { languages } from 'app/config/translation';<% } %>
import { getUser } from './user-management.reducer';
import { IRootState } from 'app/shared/reducers';

export interface IUserManagementDetailProps extends StateProps, DispatchProps, RouteComponentProps<{ login: string }> {}

export const UserManagementDetail = (props: IUserManagementDetailProps) => {
  useEffect(() => {
    props.getUser(props.match.params.login);
  }, []);

  const { user } = props;

  return (
    <div>
      <h2>
        <Translate contentKey="userManagement.detail.title">User</Translate> [<b>{user.login}</b>]
      </h2>
      <Row size="md">
        <dl className="jh-entity-details">
          <dt><Translate contentKey="userManagement.login">Login</Translate></dt>
          <dd>
            <span>{user.login}</span>&nbsp;
            {
              user.activated ? (
              <Badge color="success"><Translate contentKey="userManagement.activated">Activated</Translate></Badge>
              ) : (
              <Badge color="danger"><Translate contentKey="userManagement.deactivated">Deactivated</Translate></Badge>
              )
            }
          </dd>
          <dt><Translate contentKey="userManagement.firstName">First Name</Translate></dt>
          <dd>{user.firstName}</dd>
          <dt><Translate contentKey="userManagement.lastName">Last Name</Translate></dt>
          <dd>{user.lastName}</dd>
          <dt><Translate contentKey="userManagement.email">Email</Translate></dt>
          <dd>{user.email}</dd>
          <%_ if (enableTranslation) { _%>
          <dt><Translate contentKey="userManagement.langKey">Lang Key</Translate></dt>
          <dd>{user.langKey ? languages[user.langKey].name : undefined}</dd>
          <%_ } _%>
          <dt><Translate contentKey="userManagement.createdBy">Created By</Translate></dt>
          <dd>{user.createdBy}</dd>
          <dt><Translate contentKey="userManagement.createdDate">Created Date</Translate></dt>
          <dd>{user.createdDate ? <TextFormat value={user.createdDate} type="date" format={APP_DATE_FORMAT} blankOnInvalid /> : null}</dd>
          <dt><Translate contentKey="userManagement.lastModifiedBy">Last Modified By</Translate></dt>
          <dd>{user.lastModifiedBy}</dd>
          <dt><Translate contentKey="userManagement.lastModifiedDate">Last Modified Date</Translate></dt>
          <dd>{user.lastModifiedDate ? <TextFormat value={user.lastModifiedDate} type="date" format={APP_DATE_FORMAT} blankOnInvalid /> : null}</dd>
          <dt><Translate contentKey="userManagement.profiles">Profiles</Translate></dt>
          <dd>
            <ul className="list-unstyled">
              {
                user.authorities ? (
                user.authorities.map((authority, i) => (
                    <li key={`user-auth-${i}`}>
                      <Badge color="info">{authority}</Badge>
                    </li>
                ))) : null
              }
            </ul>
          </dd>
        </dl>
      </Row>
      <Button
        tag={Link} to="/admin/user-management" replace
        color="info"
      >
        <FontAwesomeIcon icon="arrow-left" /> <span className="d-none d-md-inline" ><Translate contentKey="entity.action.back">Back</Translate></span>
      </Button>
    </div>
  );
};

const mapStateToProps = (storeState: IRootState) => ({
  user: storeState.userManagement.user
});

const mapDispatchToProps = { getUser };

type StateProps = ReturnType<typeof mapStateToProps>;
type DispatchProps = typeof mapDispatchToProps;

export default connect(mapStateToProps, mapDispatchToProps)(UserManagementDetail);
